package cn.zgr.web;

import cn.zgr.dao.UserDAO;
import cn.zgr.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class RegServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        User user = new User();
        //读取并存储用户名，密码，邮箱
        user.setUsername(request.getParameter("username"));
        user.setPwd(request.getParameter("pwd"));
        user.setEmail(request.getParameter("email"));

        UserDAO dao = new UserDAO();

        try {
            dao.save(user);
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            response.sendRedirect("login.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            out.println("系统繁忙！");
        }
    }
}
